//
//  SEGTarget_Pay.m
//  SEGUhomeStandard
//
//  Created by 刘山 on 2018/3/24.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGTarget_Pay.h"
#import "SEGDidPaidViewController.h"
#import "SEGPayListViewController.h"
#import <WXApiObject.h>
#import "SEGPayManager.h"
#import <ICBCPaySDK/ICBCPaySDK.h>
#import "UMSPPPayUnifyPayPlugin.h"

@implementation SEGTarget_Pay

/**
 * 支付成功页面
 * @author Helen
 * @param isBillpay; //账单
 * @param  monthCard; //停车缴费
 * @param isServicePay; //有偿服务
 * @param SEGPayTypeName payTypeName; // 不同的支付类型
 * @param merchantDic
 */
-(UIViewController *)segAction_initDidPaidViewController:(NSDictionary *)params{
    return nil;
}

/**
 获取支付成功页面的Clas
 
 @param params 参数
 @return Class
 */
-(Class)segAction_getDidPayViewControllerClass:(NSDictionary *)params {
    return [SEGDidPaidViewController class];
}

/**
 * 订单-支付方式
 * @author Helen
 */
-(UIViewController *)segAction_initPayListViewController:(NSDictionary *)params{
    
    SEGCommonPayViewModel *viewModel = [self getViewModeFromJsonData:params];
    SEGPayListViewController* PayListVC = [[SEGPayListViewController alloc] initWithViewModel:viewModel];

    return PayListVC;
}

/// 发起侨城汇支付
///  把 orderId 传进，请求支付列表，代码指定支付方式(而不是UI展示)，唤起支付工具
/// @see SEGPayTypeName_QiaoChengHui
-(void)segAction_sendPayForQiaoChengHuiWithParams:(NSDictionary*)params {
    
    SEGCommonPayViewModel *viewModel = [self getViewModeFromJsonData:params];
    
    [viewModel.appointPayMethodCommand execute:nil];
    
    [viewModel.messageSubject subscribeNext:^(NSString*  _Nullable x) {
        [SEGAlertView showTmepMessageWithMessage:x];
    }];
    
}

/// 发起聚合支付
/// 发起支付的参数外部直接内传，唤起支付工具
/// @see SEGPayTypeName_CMHK_PayOnLine
-(void)segAction_sendPayForPayOnLineWithParams:(NSDictionary*)params {
    
    SEGCommonPayViewModel *viewModel = [self getViewModeFromJsonData:params];
    
    [viewModel sendToOnlinePay];
    
    [viewModel.messageSubject subscribeNext:^(NSString*  _Nullable x) {
        [SEGAlertView showTmepMessageWithMessage:x];
    }];
    
}

//toWXPayWithPayReqJsonData

- (void)segAction_initMiniProgram:(NSDictionary *)params {
    SEGCommonPayViewModel *viewModel = [self getViewModeFromJsonData:params];
    [viewModel toWXMiniProgramPay];
}


#pragma mark - Private Methods

- (void)segAction_initPayAction:(NSDictionary *)params {
    [SEGPayManager payWithParams:[params valueForKey:@"dict"] payCode:[params valueForKey:@"payCode"] payChannel:[params valueForKey:@"payChannel"]];
}


/// Json To SEGCommonPayViewModel
-(SEGCommonPayViewModel*)getViewModeFromJsonData:(NSDictionary*)params {
    SEGCommonPayViewModel *viewModel = [[SEGCommonPayViewModel alloc] init];
    
    if (params) {
        viewModel.payTypeName =  [[params valueForKey:@"payTypeName"] integerValue]; // 支付的业务类型
        viewModel.listData = [params valueForKey:@"listData"]; //支付方式列表
        viewModel.dataDic = [params valueForKey:@"dataDic"]; // 点击cell请求接口的参数
        viewModel.orderIdOracctItemIds = [params valueForKey:@"orderIdOracctItemIds"]; //账单所有的 acctItemIds
        
        SEGDidPaidViewModel *didPaidViewModel = [[SEGDidPaidViewModel alloc] init];
        didPaidViewModel.payTypeName = viewModel.payTypeName;
        didPaidViewModel.dataDic = viewModel.dataDic;
        
        viewModel.didPaidViewModel = didPaidViewModel;
    }
    
    return viewModel;
}

//e支付回调
- (void)segAction_initICBCPayBackWithParams:(NSDictionary*)params {
    NSURL *url = [NSURL URLWithString:params[@"url"]];
    [[ICBCPaySDK sharedSdk] ICBCResultBackWithUrl:url];
}

- (NSString *)segAction_initUMSPPPayWithParams:(NSDictionary *)params {
    return [NSString stringWithFormat:@"%u",[UMSPPPayUnifyPayPlugin registerApp:kWeChatAppId universalLink:kUniversalLink]];
}

- (NSString *)segAction_UMSPPPaycloudPayHandleOpenURLWithParams:(NSDictionary *)params {
    NSURL *url = [params valueForKey:@"url"];
    return [NSString stringWithFormat:@"%u",[UMSPPPayUnifyPayPlugin cloudPayHandleOpenURL:url]];
}

- (NSString *)segAction_UMSPPayHandleOpenURLWithParams:(NSDictionary *)params {
    NSURL *url = [params valueForKey:@"url"];
    id delegate = [params valueForKey:@"delegate"];
    return [NSString stringWithFormat:@"%u",[UMSPPPayUnifyPayPlugin handleOpenURL:url otherDelegate:delegate]];
}

@end
